<template>
  <template v-if="getShow">
    <LoginFormTitle class="enter-x" />
    <Form
      class="p-4 enter-x"
      :model="formData"
      :rules="getFormRules"
      ref="formRef"
      style="margin-top: 50px"
    >
      <FormItem name="mobile" class="enter-x">
        <Input
          size="large"
          v-model:value="formData.mobile"
          :maxlength="11"
          :placeholder="t('sys.login.mobile')"
        >
          <template #prefix>
            <MobileOutlined />
          </template>
        </Input>
      </FormItem>
      <FormItem name="code" class="enter-x">
        <CountdownInput
          size="large"
          v-model:value="formData.code"
          :placeholder="t('sys.login.smsCode')"
          :sendCodeApi="sendCodeApi"
        />
      </FormItem>

      <FormItem name="password" class="enter-x">
        <InputPassword
          size="large"
          visibilityToggle
          v-model:value="formData.password"
          :placeholder="t('sys.login.password')"
        >
          <template #prefix>
            <LockOutlined />
          </template>
        </InputPassword>
      </FormItem>

      <FormItem class="enter-x">
        <Button type="primary" size="large" block @click="handleReset" :loading="loading">
          {{ t('common.okText') }}
        </Button>
        <Button size="large" block class="mt-4" @click="handleBackLogin">
          {{ t('sys.login.backSignIn') }}
        </Button>
      </FormItem>
    </Form>
  </template>
</template>
<script lang="ts" setup>
  import { reactive, ref, computed, unref, onMounted } from 'vue';
  import LoginFormTitle from './LoginFormTitle.vue';
  import { Form, Input, Button } from 'ant-design-vue';
  import { LockOutlined, MobileOutlined } from '@ant-design/icons-vue';
  import { CountdownInput } from '/@/components/CountDown';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useLoginState, useFormRules, LoginStateEnum, useFormValid } from './useLogin';
  import { useUserStore } from '/@/store/modules/user';

  const FormItem = Form.Item;
  const InputPassword = Input.Password;
  const { t } = useI18n();
  const { handleBackLogin, getLoginState, setLoginState } = useLoginState();
  const { getFormRules } = useFormRules();
  const { sendCode, editPassword } = useUserStore();

  const formRef = ref();
  const { validForm } = useFormValid(formRef);
  const loading = ref(false);

  const formData = reactive({
    mobile: '',
    code: '',
    password: '',
  });

  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.RESET_PASSWORD);

  const sendCodeApi = async (): Promise<boolean> => {
    const form = unref(formRef);
    const result = await form.validateFields(['mobile']);
    if (!result) return false;
    return sendCode({ mobilePhone: formData.mobile });
  };

  const handleReset = async () => {
    const data = await validForm();
    const reslut = editPassword(data);
    if (!reslut) return;
    unref(formRef).resetFields();
    setLoginState(LoginStateEnum.LOGIN);
  };

  onMounted(() => {
    unref(formRef)?.resetFields();
  });
</script>
